package com.ghxxzx.dagl.controller;

import com.ghxxzx.dagl.entity.Dalx;
import com.ghxxzx.dagl.entity.Fg;
import com.ghxxzx.dagl.entity.User;
import com.ghxxzx.dagl.exceptions.JyException;
import com.ghxxzx.dagl.mapper.FgMapper;
import com.ghxxzx.dagl.service.FgService;
import com.ghxxzx.dagl.vo.PageQuery;
import com.ghxxzx.dagl.vo.ResultEntity;
import io.swagger.annotations.*;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Api(value = "法规录入")
@RestController
@RequestMapping("/fglr/api")
public class FgController {
    Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private FgService fgService;
    @Value("${fg.fj}")
    private String path;
    @Autowired
    private FgMapper fgMapper;

    @ApiOperation(value = "新增法规文件", notes = "添加")
    @RequestMapping(value = "addFg")
    public ResultEntity addFg(MultipartFile multipartFile, Fg fg, HttpSession httpSession) {
        try {
//            MultipartFile multipartFile = fg.getMultipartFile();
            User user = (User) httpSession.getAttribute("user");
            if (multipartFile == null) {
                throw new JyException("请选择有效的文件");
            }
            return fgService.addFg(multipartFile, user, fg);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @ApiOperation(value = "查询所有法规文件", notes = "查询")
    @RequestMapping(value = "queryAll")
    public ResultEntity queryAll() {
        try {
            return fgService.queryAll();
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @ApiOperation(value = "根据名字模糊查询法规文件", notes = "查询")
    @RequestMapping(value = "querykeyword")
    public ResultEntity querykeyword(String keyword) {
        try {
            return fgService.querykeyword(keyword);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @ApiOperation(value = "根据文件名预览法规文件", notes = "查询")
    @RequestMapping(value = "previewFg/{id}")
    public void previewFg(@PathVariable("id") Integer id, @ApiParam(hidden = true) HttpServletRequest request,
                          @ApiParam(hidden = true) HttpServletResponse response) {
        Fg fg = fgMapper.selectById(id);
        String filepath = path + fg.getName() + ".pdf";
        File file = new File(filepath);
        FileInputStream fileInputStream = null;
        ServletOutputStream out = null;
        try {
            fileInputStream = new FileInputStream(file);
            out = response.getOutputStream();
            IOUtils.copy(fileInputStream,out);
        } catch (JyException e) {
            log.error(e.getMess(), e);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


//        try {
//            return fgService.previewFg(name,request,response);
//        }  catch (JyException e) {
//            e.printStackTrace();
//            return ResultEntity.error(e.getMess());
//        } catch (Exception e){
//            e.printStackTrace();
//            return ResultEntity.error("未知异常");
//        }
    }

    @ApiOperation(value = "根据文件名下载法规文件", notes = "查询")
    @RequestMapping(value = "downloadFg")
    public void downloadFg(Integer id, @ApiParam(hidden = true) HttpServletRequest request,
                           @ApiParam(hidden = true) HttpServletResponse response) {
        try {
            Fg fg = fgMapper.selectById(id);
            String name = fg.getName();
            fgService.downloadFg(name, request, response);
        } catch (JyException e) {
            log.error(e.getMess(), e);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @ApiOperation(value = "根据id删除法规文件", notes = "删除")
    @RequestMapping(value = "deleteById")
    public ResultEntity deleteById(Integer id) {
        try {
            return fgService.deleteById(id);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }
}
